<?php 

class activityController extends appController
{
    
    public function cardList()
    {
        $activity = Model('activity');
        $page = yf_empty(intval($_GET['page']), 1, true);
        $cardList = $activity->getCardList($page);
        $this->assign('list', $cardList['result']);
        $this->assign('page', $cardList['page']);
        $this->display('cardList.html');
    }
    
    public function addCard()
    {
        $this->assign('action', 'insertCard');
        $this->display('card.html');
    }
    
    public function insertCard()
    {
        $activity = Model('activity');
        $res = array('error' => 1, 'content' => '添加失败');
        $info['card_money'] = yf_empty(intval($_POST['card_money']), '请输入价格');
        $info['card_note'] = yf_empty($_POST['card_note'], '请输入备注');
        $info['card_num'] = yf_empty(intval($_POST['card_num']), '请输入数量');
        $info['card_chance'] = yf_empty(intval($_POST['card_chance']), '请输入几率');
        $info['ordering'] = yf_empty(intval($_POST['ordering']), 0, true);
        $info['created'] = yf_time();
        
        $isExistMoney = $activity->isExistMoney($info['card_money']);
        if(!$isExistMoney)
        {
            $allChance = $activity->getAllChance($info['card_chance']);
            if($allChance)
            {
                $res['content'] = '所有的优惠券的总几率不能大于100%';
                $this->showMsg($res);    
            }
            
            $id = $activity->insertCard($info);
            if($id)
            {
                $res = array('error' => 0, 'content' => '添加成功');
            }
        }
        else
        {
            $res['content'] = '该价格已经存在';
        }
        $this->showMsg($res);
    }
    
    public function orderCard()
    {
        $activity = Model('activity');
        $res = array('error' => 1, 'content' => '');
        $id = yf_empty(intval($_POST['id']), '', true);
        $val = yf_empty(intval($_POST['val']), 0, true);
        $result = $activity->orderCard($val, $id);
        if($result)
        {
            $res = array('error' => 0, 'content' => '');
        }
        $this->showMsg($res);
    }
    
    public function editCard()
    {
        $activity = Model('activity');
        $id = yf_empty(intval($_GET['id']), '参数错误');
        $info = $activity->getCardInfo($id);
        $this->assign('info', $info);
        $this->assign('id', $id);
        $this->assign('action', 'updateCard');
        $this->display('card.html');
    }
    
    public function updateCard()
    {
        $activity = Model('activity');
        $res = array('error' => 1, 'content' => '修改失败');
        $id = yf_empty(intval($_POST['id']), '参数错误');
        $info['card_money'] = yf_empty(intval($_POST['card_money']), '请输入价格');
        $info['card_note'] = yf_empty($_POST['card_note'], '请输入备注');
        $info['card_num'] = yf_empty(intval($_POST['card_num']), '请输入数量');
        $info['card_chance'] = yf_empty(intval($_POST['card_chance']), '请输入几率');
        $info['ordering'] = yf_empty(intval($_POST['ordering']), 0, true);
        
        $isExistMoney = $activity->isExistMoney($info['card_money'], $id);
        if(!$isExistMoney)
        {
            $allChance = $activity->getAllChance($info['card_chance'], $id);
            if($allChance)
            {
                $res['content'] = '所有的优惠券的总几率不能大于100%';
                $this->showMsg($res);
            }
            
            $result = $activity->updateCard($info, $id);
            if($result)
            {
                $res = array('error' => 0, 'content' => '修改成功');
            }
        }
        else
        {
            $res['content'] = '该价格已经存在';
        }
        $this->showMsg($res);
    }
    
    public function delCard()
    {
        $activity = Model('activity');
        $res = array('error' => 1, 'content' => '删除失败');
        $id = yf_empty(intval($_REQUEST['id']), '参数错误');
        $result = $activity->delCard($id);
        if($result)
        {
            $res = array('error' => 0, 'content' => '删除成功');
        }
        $this->showMsg($res);
    }
    
    public function cardLog()
    {
        $activity = Model('activity');
        
        $allCount = $activity->getAllRecode();
        $daoLimit = 1000;
        $num = ceil($allCount / $daoLimit);
        $arr = array();
        if($num > 0)
        {
            $start = 1;
            for($i = 0; $i < $num; $i++)
            {
                $arr[$i]['num'] = $i + 1;
                $arr[$i]['start'] = $start;
                $arr[$i]['end'] = $start + $daoLimit - 1;
                $start += $daoLimit;
            }
        }
        $this->assign('arr', $arr);
        
        $page = yf_empty(intval($_GET['page']), 1, true);
        $list = $activity->getPageLog($page);
        $this->assign('list', $list['result']);
        $this->assign('page', $list['page']);
        $this->display('log.html');
    }
    
    public function daoLog()
    {
        $page = yf_empty(intval($_POST['num']), 0, true);
        $limit = 1000;
        $activity = Model('activity');
        $info = $activity->getDaoLog($page, $limit);
        if($info)
        {
            $table = array('序号', 'openid', '昵称', '价格', 'ip', '日期');
            yf_daochu($table, $info, date('YmdHi', yf_time())."记录");
        }
        $this->cardLog();
    }
    
    public function areaLog()
    {
        $activity = Model('activity');
        $list = $activity->getAreaLog();
        $this->assign('list', $list);
        
        $count = $activity->getJoinNum();
        $this->assign('count', $count);
        $this->display('areaList.html');
    }
    
    public function viewDetail()
    {
        $id = yf_empty(intval($_GET['id']), '参数错误');
        $activity = Model('activity');
        $list = $activity->getCityLog($id);
        $this->assign('list', $list);
        $this->display('cityLog.html');
    }
    
}